<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us">
<head>
  <link href="//gmpg.org/xfn/11" rel="profile">
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="generator" content="Hugo 0.69.0" />

  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>用户注册逻辑 &middot; 郝朝帅的博客</title>

  
  <link type="text/css" rel="stylesheet" href="/blog/css/print.css" media="print">
  <link type="text/css" rel="stylesheet" href="/blog/css/poole.css">
  <link type="text/css" rel="stylesheet" href="/blog/css/syntax.css">
  <link type="text/css" rel="stylesheet" href="/blog/css/hyde.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Abril+Fatface|PT+Sans:400,400i,700">


  
  <link rel="apple-touch-icon-precomposed" sizes="144x144" href="/apple-touch-icon-144-precomposed.png">
  <link rel="shortcut icon" href="/favicon.png">

  
  
</head>

  <body class="theme-base-0f ">
  <aside class="sidebar">
  <div class="container sidebar-sticky">
    <div class="sidebar-about">
      <a href="/blog/"><h1>郝朝帅的博客</h1></a>
      <p class="lead">
       我的个人博客 
      </p>
    </div>

    <nav>
      <ul class="sidebar-nav">
        <li><a href="/blog/">Home</a> </li>
        
      </ul>
    </nav>

    <p>&copy; 2020. All rights reserved. </p>
  </div>
</aside>

    <main class="content container">
    <div class="post">
  <h1>用户注册逻辑</h1>
  <time datetime=2020-04-24T20:05:06&#43;0800 class="post-date">Fri, Apr 24, 2020</time>
  <p>#用户注册逻辑</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">
<span style="color:#75715e"># 导入视图类</span>
<span style="color:#f92672">from</span> rest_framework.views <span style="color:#f92672">import</span> APIView

<span style="color:#75715e"># 导入响应模块</span>
<span style="color:#f92672">from</span> rest_framework.response <span style="color:#f92672">import</span> Response

<span style="color:#75715e"># 导入redis模块</span>
<span style="color:#f92672">import</span> redis

<span style="color:#75715e">#定义注册类</span>
<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">Register</span>(APIView):
    <span style="color:#75715e"># 请求模式为GET</span>
    <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">get</span>(self,request):

        <span style="color:#75715e">#接收参数</span>
        username <span style="color:#f92672">=</span> request<span style="color:#f92672">.</span>GET<span style="color:#f92672">.</span>get(<span style="color:#e6db74">&#39;username&#39;</span>,<span style="color:#e6db74">&#39;未收到用户名&#39;</span>)
        password <span style="color:#f92672">=</span> request<span style="color:#f92672">.</span>GET<span style="color:#f92672">.</span>get(<span style="color:#e6db74">&#39;password&#39;</span>,<span style="color:#e6db74">&#39;未收到密码&#39;</span>)
        code <span style="color:#f92672">=</span> request<span style="color:#f92672">.</span>GET<span style="color:#f92672">.</span>get(<span style="color:#e6db74">&#39;code&#39;</span>,<span style="color:#e6db74">&#39;未收到验证码&#39;</span>)


        <span style="color:#75715e">#从session中获取验证码</span>
        session_code <span style="color:#f92672">=</span> request<span style="color:#f92672">.</span>session<span style="color:#f92672">.</span>get(<span style="color:#e6db74">&#39;code&#39;</span>,None)

        <span style="color:#66d9ef">print</span>(session_code)

        <span style="color:#75715e">#从redis中获取生成好的验证码</span>
        mycode <span style="color:#f92672">=</span> redis<span style="color:#f92672">.</span>Redis<span style="color:#f92672">.</span>get(<span style="color:#e6db74">&#39;code&#39;</span>)
        <span style="color:#75715e">#转码</span>
        mycode <span style="color:#f92672">=</span> mycode<span style="color:#f92672">.</span>decode(<span style="color:#e6db74">&#39;utf-8&#39;</span>)

        <span style="color:#75715e">#print(mycode)</span>

        <span style="color:#75715e">#判断验证码是否输入正确</span>
        <span style="color:#66d9ef">if</span> code <span style="color:#f92672">!=</span> mycode:
            res <span style="color:#f92672">=</span> {}
            res[<span style="color:#e6db74">&#39;code&#39;</span>] <span style="color:#f92672">=</span> <span style="color:#ae81ff">405</span>
            res[<span style="color:#e6db74">&#39;message&#39;</span>] <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;验证码输入错误&#39;</span>
            <span style="color:#66d9ef">return</span> Response(res)

        <span style="color:#75715e">#查询数据表里有没有该用户,User为数据表</span>
        user <span style="color:#f92672">=</span> User<span style="color:#f92672">.</span>objects<span style="color:#f92672">.</span>filter(username<span style="color:#f92672">=</span>username)<span style="color:#f92672">.</span>first()

        <span style="color:#66d9ef">if</span> user:
            res <span style="color:#f92672">=</span> {}
            res[<span style="color:#e6db74">&#39;code&#39;</span>] <span style="color:#f92672">=</span> <span style="color:#ae81ff">405</span>
            res[<span style="color:#e6db74">&#39;message&#39;</span>] <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;用户已经存在&#39;</span>
            <span style="color:#66d9ef">return</span> Response(res)

        <span style="color:#75715e">#进行入库操作，User为数据表</span>
        user <span style="color:#f92672">=</span> User(username<span style="color:#f92672">=</span>username,password<span style="color:#f92672">=</span>password)
        <span style="color:#75715e">#保存</span>
        user<span style="color:#f92672">.</span>save()

        <span style="color:#75715e">#返回结果</span>
        <span style="color:#66d9ef">return</span> Response({<span style="color:#e6db74">&#39;code&#39;</span>:<span style="color:#ae81ff">200</span>,<span style="color:#e6db74">&#39;message&#39;</span>:<span style="color:#e6db74">&#39;注册成功&#39;</span>})

</code></pre></div>
</div>


    </main>

    
  </body>
</html>
